package jrain.fw.validate;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import jrain.fw.core.business.BusinessConst;
import jrain.fw.core.business.BusinessInterceptor;
import jrain.fw.core.business.BusinessRequest;
import jrain.fw.core.business.BusinessResponse;
import jrain.fw.core.business.Interceptor;
import jrain.fw.core.consts.Consts;
import jrain.fw.core.log.Logger;
import jrain.fw.core.log.LoggerFactory;
import jrain.fw.core.utils.FwUtils;
import jrain.utils.collection.CollectionUtils;
import jrain.utils.collection.RMap;
import jrain.utils.consts.BaseConst;
import jrain.utils.date.DateUtils;
import jrain.utils.lang.LangUtils;
import jrain.utils.lang.StringUtils;

@Interceptor(name = "validate")
public class FwValidateService implements BusinessInterceptor {

	private static final Logger logger = LoggerFactory.getLogger(FwValidateService.class);
	private static final String NUM_9 = "9";
	private static final String NUM_0 = "0";

	public BusinessResponse handler(BusinessRequest request) {
		BusinessResponse response = new BusinessResponse();
		List<Map<String, Object>> rlues = getParamRlues(request);
		if (rlues != null) {
			for (Map<String, Object> map : rlues) {
				String paramKey = StringUtils.trimNull(map.get(Consts.CFG_PARAM_KEY));
				String paramType = StringUtils.trimNull(map.get(Consts.CFG_PARAM_TYPE));
				String paramDvalue = StringUtils.trimNull(map.get(Consts.CFG_PARAM_DVALUE));
				String paramRule = StringUtils.trimNull(map.get(Consts.CFG_PARAM_RULE));
				// 只有输入参数--或者有自定义校验规则--或者有默认值的数据
				boolean b = request.getData().keySet().contains(paramKey);
				if (paramDvalue.length() > 0 || paramRule.length() > 0 || b) {
					if (Consts.CFG_PARAM_TYPE_INT.equals(paramType)) {
						intRlue(request, response, map);
					} else if (Consts.CFG_PARAM_TYPE_LONG.equals(paramType)) {
						longRlue(request, response, map);
					} else if (Consts.CFG_PARAM_TYPE_MONEY.equals(paramType)) {
						moneyRlue(request, response, map);
					} else if (Consts.CFG_PARAM_TYPE_DATE.equals(paramType)) {
						dateRlue(request, response, map);
					} else if (Consts.CFG_PARAM_TYPE_STRING.equals(paramType)) {
						stringRlue(request, response, map);
					}
				}
				if (response.getCode() != BusinessConst.RES_CODE_SUCCESS) {
					break;
				}
			}
		}
		// 参数校验
		return response;
	}

	@SuppressWarnings("unchecked")
	protected List<Map<String, Object>> getParamRlues(BusinessRequest request) {
		return (List<Map<String, Object>>) FwUtils.getCfgService().getCfgValue(Consts.CFG_TYPE_PARAM, request.getKey(),
				null);
	}

	// 基础校验
	protected void intRlue(BusinessRequest request, BusinessResponse response, Map<String, Object> map) {
		int dvalue = LangUtils.parseInt(map.get(Consts.CFG_PARAM_DVALUE));
		String paramKey = StringUtils.trimNull(map.get(Consts.CFG_PARAM_KEY));
		String paramName = StringUtils.trimNull(map.get(Consts.CFG_PARAM_NAME));
		String paramEmpty = StringUtils.trimNull(map.get(Consts.CFG_PARAM_EMPTY), Consts.CFG_PARAM_EMPTY_Y);
		String paramLengthStr = StringUtils.trimNull(map.get(Consts.CFG_PARAM_LENGTH));
		List<String> paramLengths = CollectionUtils.valueOfList(paramLengthStr);
		String paramRangeStr = StringUtils.trimNull(map.get(Consts.CFG_PARAM_RANGE));
		List<String> ranges = CollectionUtils.valueOfList(paramRangeStr);
		String paramDict = StringUtils.trimNull(map.get(Consts.CFG_PARAM_DICT));
		// 获取数值
		String valueStr = StringUtils.trimNull(request.getData(paramKey));
		int value = dvalue;
		if (valueStr.length() > 0) {
			try {
				value = Integer.parseInt(valueStr);
			} catch (NumberFormatException e) {
				// reutrn
				response.setCode(BusinessConst.RES_CODE_ERROR_BUIS_PARAM);
				response.setMsg(paramName + "数据格式不合法！");
				response.putExtData(Consts.VALIDATE_RS_KEY, paramKey);
				response.putExtData(Consts.VALIDATE_RS_TYPE, Consts.VALIDATE_RS_TYPE_BASE);
				return;
			}
		}

		// 非空校验
		if (Consts.CFG_PARAM_EMPTY_N.equals(paramEmpty) && value == 0) {
			// reutrn
			response.setCode(BusinessConst.RES_CODE_ERROR_BUIS_PARAM);
			response.setMsg(paramName + "不能为空！");
			response.putExtData(Consts.VALIDATE_RS_KEY, paramKey);
			response.putExtData(Consts.VALIDATE_RS_TYPE, Consts.VALIDATE_RS_TYPE_EMPTY);
			return;
		} else if (Consts.CFG_PARAM_EMPTY_N1.equals(paramEmpty) && valueStr.length() == 0) {
			response.setCode(BusinessConst.RES_CODE_ERROR_BUIS_PARAM);
			response.setMsg(paramName + "不能为空！");
			response.putExtData(Consts.VALIDATE_RS_KEY, paramKey);
			response.putExtData(Consts.VALIDATE_RS_TYPE, Consts.VALIDATE_RS_TYPE_EMPTY);
			return;
		}
		// 长度校验
		if (paramLengths.size() == 1 || paramLengths.size() == 2) {
			int minLength = 0;
			boolean minType = true;
			int maxLength = 0;
			boolean maxType = true;
			if (paramLengths.size() == 1) {
				maxLength = LangUtils.parseInt(paramLengths.get(0));
			} else {
				String minStr = paramLengths.get(0);
				if (minStr.startsWith(BaseConst.LEFT_XKH)) {
					minLength = LangUtils.parseInt(minStr.substring(1));
					minType = false;
				} else if (minStr.startsWith(BaseConst.LEFT_DKH)) {
					minLength = LangUtils.parseInt(minStr.substring(1));
				} else {
					minLength = LangUtils.parseInt(minStr);
				}
				String maxStr = paramLengths.get(1);
				if (maxStr.endsWith(BaseConst.RIGHT_XKH)) {
					maxLength = LangUtils.parseInt(maxStr.substring(1));
					maxType = false;
				} else if (maxStr.endsWith(BaseConst.RIGHT_DKH)) {
					maxLength = LangUtils.parseInt(maxStr.substring(1));
				} else {
					maxLength = LangUtils.parseInt(maxStr);
				}
			}
			// 无效规则，最大小于最小
			boolean brlue = true;
			if (maxLength < minLength) {
				brlue = false;
			}
			// 无效规则，最大小于最小
			else if (maxLength == minLength && maxLength == 0) {
				brlue = false;
			}
			// 无效规则，最大等于最新，存在不包含
			else if (maxLength == minLength && (minType || maxType) && maxLength != 0) {
				brlue = false;
			}

			if (brlue) {
				boolean bvalidate = true;
				if (minType) {
					bvalidate = bvalidate && value >= minLength;
				} else {
					bvalidate = bvalidate && value > minLength;
				}
				if (maxType) {
					bvalidate = bvalidate && value <= maxLength;
				} else {
					bvalidate = bvalidate && value < maxLength;
				}
				if (!bvalidate) {
					response.setCode(BusinessConst.RES_CODE_ERROR_BUIS_PARAM);
					response.setMsg(paramName + "数据限制超长！");
					response.putExtData(Consts.VALIDATE_RS_KEY, paramKey);
					response.putExtData(Consts.VALIDATE_RS_TYPE, Consts.VALIDATE_RS_TYPE_LENGHT);
					response.putExtData(Consts.VALIDATE_RS_LENGTH, paramLengthStr);
					return;
				}
			}
		}
		// 数据范围校验
		if (value != 0 && ranges.size() > 0) {
			if (!ranges.contains(String.valueOf(value))) {
				response.setCode(BusinessConst.RES_CODE_ERROR_BUIS_PARAM);
				response.setMsg(paramName + "数据不在指定的范围内");
				response.putExtData(Consts.VALIDATE_RS_KEY, paramKey);
				response.putExtData(Consts.VALIDATE_RS_TYPE, Consts.VALIDATE_RS_TYPE_RANGE);
				response.putExtData(Consts.VALIDATE_RS_RANGE, paramRangeStr);
			}
			return;
		}
		// 字典
		if (value != 0 && paramDict.length() > 0) {
			RMap<String, Object> propMap = FwUtils.getCfgService().getCfgMap(paramDict, null);
			if (propMap != null) {
				if (!propMap.keySet().contains(String.valueOf(value))) {
					response.setCode(BusinessConst.RES_CODE_ERROR_BUIS_PARAM);
					response.setMsg(paramName + "数据不在指定的字典内");
					response.putExtData(Consts.VALIDATE_RS_KEY, paramKey);
					response.putExtData(Consts.VALIDATE_RS_TYPE, Consts.VALIDATE_RS_TYPE_RANGE);
					response.putExtData(Consts.VALIDATE_RS_DICT, paramDict);
				}
			}
			return;
		}
		// 规则校验
		paramRlue(request, response, map, value);
		request.putData(paramKey, value);
	}

	// 基础校验
	protected void longRlue(BusinessRequest request, BusinessResponse response, Map<String, Object> map) {
		String dvalueStr = StringUtils.trimNull(map.get(Consts.CFG_PARAM_DVALUE));
		String paramKey = StringUtils.trimNull(map.get(Consts.CFG_PARAM_KEY));
		if ("d_user".equals(dvalueStr)) {
			long userId = LangUtils.parseLong(request.getExtData(BusinessConst.EXT_USER_ID));
			request.putData(paramKey, userId);
			return;
		}
		long dvalue = LangUtils.parseInt(dvalueStr);
		String paramName = StringUtils.trimNull(map.get(Consts.CFG_PARAM_NAME));
		String paramEmpty = StringUtils.trimNull(map.get(Consts.CFG_PARAM_EMPTY), Consts.CFG_PARAM_EMPTY_Y);
		String paramLengthStr = StringUtils.trimNull(map.get(Consts.CFG_PARAM_LENGTH));
		List<String> paramLengths = CollectionUtils.valueOfList(paramLengthStr);
		String paramRangeStr = StringUtils.trimNull(map.get(Consts.CFG_PARAM_RANGE));
		List<String> ranges = CollectionUtils.valueOfList(paramRangeStr);
		String paramDict = StringUtils.trimNull(map.get(Consts.CFG_PARAM_DICT));
		// 获取数值
		String valueStr = StringUtils.trimNull(request.getData(paramKey));
		long value = dvalue;
		if (valueStr.length() > 0) {
			try {
				value = Long.parseLong(valueStr);
			} catch (NumberFormatException e) {
				// reutrn
				response.setCode(BusinessConst.RES_CODE_ERROR_BUIS_PARAM);
				response.setMsg(paramName + "数据格式不合法！");
				response.putExtData(Consts.VALIDATE_RS_KEY, paramKey);
				response.putExtData(Consts.VALIDATE_RS_TYPE, Consts.VALIDATE_RS_TYPE_BASE);
				return;
			}
		}
		// 非空校验
		if (Consts.CFG_PARAM_EMPTY_N.equals(paramEmpty) && value == 0) {
			// reutrn
			response.setCode(BusinessConst.RES_CODE_ERROR_BUIS_PARAM);
			response.setMsg(paramName + "不能为空！");
			response.putExtData(Consts.VALIDATE_RS_KEY, paramKey);
			response.putExtData(Consts.VALIDATE_RS_TYPE, Consts.VALIDATE_RS_TYPE_EMPTY);
			return;
		} else if (Consts.CFG_PARAM_EMPTY_N1.equals(paramEmpty) && valueStr.length() == 0) {
			response.setCode(BusinessConst.RES_CODE_ERROR_BUIS_PARAM);
			response.setMsg(paramName + "不能为空！");
			response.putExtData(Consts.VALIDATE_RS_KEY, paramKey);
			response.putExtData(Consts.VALIDATE_RS_TYPE, Consts.VALIDATE_RS_TYPE_EMPTY);
			return;
		}
		// 长度校验
		if (paramLengths.size() == 1 || paramLengths.size() == 2) {
			long minLength = 0;
			boolean minType = true;
			long maxLength = 0;
			boolean maxType = true;
			if (paramLengths.size() == 1) {
				maxLength = LangUtils.parseLong(paramLengths.get(0));
			} else {
				String minStr = paramLengths.get(0);
				if (minStr.startsWith(BaseConst.LEFT_XKH)) {
					minLength = LangUtils.parseLong(minStr.substring(1));
					minType = false;
				} else if (minStr.startsWith(BaseConst.LEFT_DKH)) {
					minLength = LangUtils.parseLong(minStr.substring(1));
				} else {
					minLength = LangUtils.parseLong(minStr);
				}
				String maxStr = paramLengths.get(1);
				if (maxStr.endsWith(BaseConst.RIGHT_XKH)) {
					maxLength = LangUtils.parseLong(maxStr.substring(1));
					maxType = false;
				} else if (maxStr.endsWith(BaseConst.RIGHT_DKH)) {
					maxLength = LangUtils.parseLong(maxStr.substring(1));
				} else {
					maxLength = LangUtils.parseLong(maxStr);
				}
			}
			boolean brlue = true;
			// 无效规则，最大小于最小
			if (maxLength < minLength) {
				brlue = false;
			}
			// 无效规则，最大小于最小
			else if (maxLength == minLength && maxLength == 0) {
				brlue = false;
			}
			// 无效规则，最大等于最新，存在不包含
			else if (maxLength == minLength && (minType || maxType) && maxLength != 0) {
				brlue = false;
			}

			if (brlue) {
				boolean bvalidate = true;
				if (minType) {
					bvalidate = bvalidate && value >= minLength;
				} else {
					bvalidate = bvalidate && value > minLength;
				}
				if (maxType) {
					bvalidate = bvalidate && value <= maxLength;
				} else {
					bvalidate = bvalidate && value < maxLength;
				}
				if (!bvalidate) {
					response.setCode(BusinessConst.RES_CODE_ERROR_BUIS_PARAM);
					response.setMsg(paramName + "数值不在允许的范围内！");
					response.putExtData(Consts.VALIDATE_RS_KEY, paramKey);
					response.putExtData(Consts.VALIDATE_RS_TYPE, Consts.VALIDATE_RS_TYPE_LENGHT);
					response.putExtData(Consts.VALIDATE_RS_LENGTH, paramLengthStr);
					return;
				}
			}
		}
		// 数据范围校验
		if (value != 0 && ranges.size() > 0) {
			if (!ranges.contains(String.valueOf(value))) {
				response.setCode(BusinessConst.RES_CODE_ERROR_BUIS_PARAM);
				response.setMsg(paramName + "数据不在指定的范围内");
				response.putExtData(Consts.VALIDATE_RS_KEY, paramKey);
				response.putExtData(Consts.VALIDATE_RS_TYPE, Consts.VALIDATE_RS_TYPE_RANGE);
				response.putExtData(Consts.VALIDATE_RS_LENGTH, paramRangeStr);
			}
			return;
		}
		// 字典
		if (value != 0 && paramDict.length() > 0) {
			RMap<String, Object> propMap = FwUtils.getCfgService().getCfgMap(paramDict, null);
			if (propMap != null) {
				if (!propMap.keySet().contains(String.valueOf(value))) {
					response.setCode(BusinessConst.RES_CODE_ERROR_BUIS_PARAM);
					response.setMsg(paramName + "数据不在指定的字典内");
					response.putExtData(Consts.VALIDATE_RS_KEY, paramKey);
					response.putExtData(Consts.VALIDATE_RS_TYPE, Consts.VALIDATE_RS_TYPE_DICT);
					response.putExtData(Consts.VALIDATE_RS_DICT, paramDict);
				}
			}
			return;
		}
		// 规则校验
		paramRlue(request, response, map, value);
		request.putData(paramKey, value);
	}

	// 基础校验
	protected void moneyRlue(BusinessRequest request, BusinessResponse response, Map<String, Object> map) {
		BigDecimal dvalue = LangUtils.parseBigDecimal(map.get(Consts.CFG_PARAM_DVALUE));
		String paramKey = StringUtils.trimNull(map.get(Consts.CFG_PARAM_KEY));
		String paramName = StringUtils.trimNull(map.get(Consts.CFG_PARAM_NAME));
		String paramEmpty = StringUtils.trimNull(map.get(Consts.CFG_PARAM_EMPTY), Consts.CFG_PARAM_EMPTY_Y);
		String paramLengthStr = StringUtils.trimNull(map.get(Consts.CFG_PARAM_RANGE));
		List<String> paramLengths = CollectionUtils.valueOfList(paramLengthStr);
		int paramLength = LangUtils.parseInt(Consts.CFG_PARAM_LENGTH);
		int paramScale = LangUtils.parseInt(map.get(Consts.CFG_PARAM_SCALE));
		// 获取数值
		String valueStr = StringUtils.trimNull(request.getData(paramKey));
		BigDecimal value = dvalue;
		if (valueStr.length() > 0) {
			try {
				value = new BigDecimal(valueStr);
			} catch (NumberFormatException e) {
				// reutrn
				response.setCode(BusinessConst.RES_CODE_ERROR_BUIS_PARAM);
				response.setMsg(paramName + "数据格式不合法！");
				response.putExtData(Consts.VALIDATE_RS_KEY, paramKey);
				response.putExtData(Consts.VALIDATE_RS_TYPE, Consts.VALIDATE_RS_TYPE_BASE);
				return;
			}
		}
		// 数据格式校验
		if (paramLength > 0) {
			StringBuilder maxSb = new StringBuilder();
			StringBuilder minSb = new StringBuilder();
			minSb.append(BaseConst.ZHX);
			for (int i = 0; i < paramLength; i++) {
				maxSb.append(NUM_9);
			}
			for (int i = 1; i < paramLength; i++) {
				minSb.append(NUM_9);
			}
			if (paramScale > 0) {
				maxSb.append(BaseConst.POINT);
				minSb.append(BaseConst.POINT);
			}
			for (int i = 0; i < paramScale; i++) {
				maxSb.append(NUM_9);
				minSb.append(NUM_9);
			}
			String minSbStr = minSb.toString();
			if (BaseConst.ZHX.equals(minSbStr)) {
				minSbStr = NUM_0;
			}
			String maxSbStr = maxSb.toString();
			BigDecimal maxValue = new BigDecimal(maxSbStr);
			BigDecimal minValue = new BigDecimal(minSbStr);
			if (!(value.compareTo(minValue) >= 0 && value.compareTo(maxValue) <= 0)) {
				// reutrn
				response.setCode(BusinessConst.RES_CODE_ERROR_BUIS_PARAM);
				response.setMsg(paramName + "数据格式不合法！");
				response.putExtData(Consts.VALIDATE_RS_KEY, paramKey);
				response.putExtData(Consts.VALIDATE_RS_TYPE, Consts.VALIDATE_RS_TYPE_BASE);
				return;
			}
		}
		// 非空校验
		if (Consts.CFG_PARAM_EMPTY_N.equals(paramEmpty) && value.compareTo(new BigDecimal(0)) == 0) {
			// reutrn
			response.setCode(BusinessConst.RES_CODE_ERROR_BUIS_PARAM);
			response.setMsg(paramName + "不能为空！");
			response.putExtData(Consts.VALIDATE_RS_KEY, paramKey);
			response.putExtData(Consts.VALIDATE_RS_TYPE, Consts.VALIDATE_RS_TYPE_EMPTY);
			return;
		} else if (Consts.CFG_PARAM_EMPTY_N1.equals(paramEmpty) && valueStr.length() == 0) {
			response.setCode(BusinessConst.RES_CODE_ERROR_BUIS_PARAM);
			response.setMsg(paramName + "不能为空！");
			response.putExtData(Consts.VALIDATE_RS_KEY, paramKey);
			response.putExtData(Consts.VALIDATE_RS_TYPE, Consts.VALIDATE_RS_TYPE_EMPTY);
			return;
		}
		// 长度校验
		if (value.compareTo(new BigDecimal(0)) != 0 && paramLengths.size() == 1 || paramLengths.size() == 2) {
			BigDecimal minLength = new BigDecimal(0);
			boolean minType = true;
			BigDecimal maxLength = new BigDecimal(0);
			boolean maxType = true;
			if (paramLengths.size() == 1) {
				maxLength = LangUtils.parseBigDecimal(paramLengths.get(0));
			} else {
				String minStr = paramLengths.get(0);
				if (minStr.startsWith(BaseConst.LEFT_XKH)) {
					minLength = LangUtils.parseBigDecimal(minStr.substring(1));
					minType = false;
				} else if (minStr.startsWith(BaseConst.LEFT_DKH)) {
					minLength = LangUtils.parseBigDecimal(minStr.substring(1));
				} else {
					minLength = LangUtils.parseBigDecimal(minStr);
				}
				String maxStr = paramLengths.get(1);
				if (maxStr.endsWith(BaseConst.RIGHT_XKH)) {
					maxLength = LangUtils.parseBigDecimal(maxStr.substring(1));
					maxType = false;
				} else if (maxStr.endsWith(BaseConst.RIGHT_DKH)) {
					maxLength = LangUtils.parseBigDecimal(maxStr.substring(1));
				} else {
					maxLength = LangUtils.parseBigDecimal(maxStr);
				}
			}
			// 无效规则，最大小于最小
			boolean brlue = true;
			if (maxLength.compareTo(minLength) < 0) {
				brlue = false;
			}
			// 无效规则，最大等于最新，存在不包含
			else if (maxLength == minLength && (minType || maxType) && maxLength.compareTo(new BigDecimal(0)) != 0) {
				brlue = false;
			}
			if (!brlue) {
				logger.warn(request, "invalid check!,key={},length={}", paramKey, paramLengthStr);
			}
			if (brlue) {
				boolean bvalidate = true;
				if (minType) {
					bvalidate = bvalidate && value.compareTo(minLength) >= 0;
				} else {
					bvalidate = bvalidate && value.compareTo(minLength) > 0;
				}
				if (maxType) {
					bvalidate = bvalidate && value.compareTo(minLength) <= 0;
				} else {
					bvalidate = bvalidate && value.compareTo(minLength) < 0;
				}
				if (!bvalidate) {
					response.setCode(BusinessConst.RES_CODE_ERROR_BUIS_PARAM);
					response.setMsg(paramName + "数值不在允许的范围内！");
					response.putExtData(Consts.VALIDATE_RS_KEY, paramKey);
					response.putExtData(Consts.VALIDATE_RS_TYPE, Consts.VALIDATE_RS_TYPE_RANGE);
					response.putExtData(Consts.VALIDATE_RS_RANGE, paramLengthStr);
					return;
				}
			}
		}
		// 规则校验
		paramRlue(request, response, map, value);
		request.putData(paramKey, value);
	}

	// 基础校验
	protected void stringRlue(BusinessRequest request, BusinessResponse response, Map<String, Object> map) {
		String dvalue = StringUtils.trimNull(map.get(Consts.CFG_PARAM_DVALUE));
		String paramKey = StringUtils.trimNull(map.get(Consts.CFG_PARAM_KEY));
		String paramName = StringUtils.trimNull(map.get(Consts.CFG_PARAM_NAME));
		String paramEmpty = StringUtils.trimNull(map.get(Consts.CFG_PARAM_EMPTY), Consts.CFG_PARAM_EMPTY_Y);
		String paramLengthStr = StringUtils.trimNull(map.get(Consts.CFG_PARAM_LENGTH));
		List<String> paramLengths = CollectionUtils.valueOfList(paramLengthStr);
		String paramRangeStr = StringUtils.trimNull(map.get(Consts.CFG_PARAM_RANGE));
		List<String> ranges = CollectionUtils.valueOfList(paramRangeStr);
		String paramDict = StringUtils.trimNull(map.get(Consts.CFG_PARAM_DICT));
		// 获取数值
		String value = StringUtils.trimNull(request.getData(paramKey), dvalue);
		// 非空校验
		if (Consts.CFG_PARAM_EMPTY_N.equals(paramEmpty) && value.length() == 0) {
			// reutrn
			response.setCode(BusinessConst.RES_CODE_ERROR_BUIS_PARAM);
			response.setMsg(paramName + "不能为空！");
			response.putExtData(Consts.VALIDATE_RS_KEY, paramKey);
			response.putExtData(Consts.VALIDATE_RS_TYPE, Consts.VALIDATE_RS_TYPE_EMPTY);
			return;
		} else if (Consts.CFG_PARAM_EMPTY_N1.equals(paramEmpty) && StringUtils.isEmpty(value)) {
			response.setCode(BusinessConst.RES_CODE_ERROR_BUIS_PARAM);
			response.setMsg(paramName + "不能为空！");
			response.putExtData(Consts.VALIDATE_RS_KEY, paramKey);
			response.putExtData(Consts.VALIDATE_RS_TYPE, Consts.VALIDATE_RS_TYPE_EMPTY);
			return;
		}
		// 长度校验
		if (value.length() != 0 && paramLengths.size() == 1 || paramLengths.size() == 2) {
			int minLength = 0;
			boolean minType = true;
			int maxLength = 0;
			boolean maxType = true;
			if (paramLengths.size() == 1) {
				maxLength = LangUtils.parseInt(paramLengths.get(0));
			} else {
				String minStr = paramLengths.get(0);
				if (minStr.startsWith(BaseConst.LEFT_XKH)) {
					minLength = LangUtils.parseInt(minStr.substring(1));
					minType = false;
				} else if (minStr.startsWith(BaseConst.LEFT_DKH)) {
					minLength = LangUtils.parseInt(minStr.substring(1));
				} else {
					minLength = LangUtils.parseInt(minStr);
				}
				String maxStr = paramLengths.get(1);
				if (maxStr.endsWith(BaseConst.RIGHT_XKH)) {
					maxLength = LangUtils.parseInt(maxStr.substring(1));
					maxType = false;
				} else if (maxStr.endsWith(BaseConst.RIGHT_DKH)) {
					maxLength = LangUtils.parseInt(maxStr.substring(1));
				} else {
					maxLength = LangUtils.parseInt(maxStr);
				}
			}
			// 无效规则，最大小于最小
			boolean brlue = true;
			if (maxLength < minLength) {
				brlue = false;
			}
			// 无效规则，最大等于最新，存在不包含
			else if (maxLength == minLength && (minType || maxType) && maxLength != 0) {
				brlue = false;
			}
			if (!brlue) {
				logger.warn(request, "invalid check!,key={},length={}", paramKey, paramLengthStr);
			}
			if (brlue) {
				boolean bvalidate = true;
				if (minType) {
					bvalidate = bvalidate && value.length() >= minLength;
				} else {
					bvalidate = bvalidate && value.length() > minLength;
				}
				if (maxType) {
					bvalidate = bvalidate && value.length() <= maxLength;
				} else {
					bvalidate = bvalidate && value.length() < maxLength;
				}
				if (!bvalidate) {
					response.setCode(BusinessConst.RES_CODE_ERROR_BUIS_PARAM);
					response.setMsg(paramName + "数据限制超长！");
					response.putExtData(Consts.VALIDATE_RS_KEY, paramKey);
					response.putExtData(Consts.VALIDATE_RS_TYPE, Consts.VALIDATE_RS_TYPE_LENGHT);
					response.putExtData(Consts.VALIDATE_RS_LENGTH, paramLengthStr);
					return;
				}
			}
		}
		// 数据范围校验
		if (value.length() != 0 && ranges.size() > 0) {
			if (!ranges.contains(value)) {
				response.setCode(BusinessConst.RES_CODE_ERROR_BUIS_PARAM);
				response.setMsg(paramName + "数据不在指定的范围内");
				response.putExtData(Consts.VALIDATE_RS_KEY, paramKey);
				response.putExtData(Consts.VALIDATE_RS_TYPE, Consts.VALIDATE_RS_TYPE_RANGE);
				response.putExtData(Consts.VALIDATE_RS_RANGE, paramRangeStr);
			}
			return;
		}
		// 字典
		if (value.length() != 0 && paramDict.length() > 0) {
			RMap<String, Object> propMap = FwUtils.getCfgService().getCfgMap(paramDict, null);
			if (propMap != null) {
				if (!propMap.keySet().contains(value)) {
					response.setCode(BusinessConst.RES_CODE_ERROR_BUIS_PARAM);
					response.setMsg(paramName + "数据不在指定的字典内");
					response.putExtData(Consts.VALIDATE_RS_KEY, paramKey);
					response.putExtData(Consts.VALIDATE_RS_TYPE, Consts.VALIDATE_RS_TYPE_DICT);
					response.putExtData(Consts.VALIDATE_RS_DICT, paramDict);
				}
			}
			return;
		}
		// 规则校验
		paramRlue(request, response, map, value);
		request.putData(paramKey, value);
	}

	// 基础校验
	protected void dateRlue(BusinessRequest request, BusinessResponse response, Map<String, Object> map) {
		String dvalue = StringUtils.trimNull(map.get(Consts.CFG_PARAM_DVALUE));
		String paramKey = StringUtils.trimNull(map.get(Consts.CFG_PARAM_KEY));
		String paramName = StringUtils.trimNull(map.get(Consts.CFG_PARAM_NAME));
		String paramEmpty = StringUtils.trimNull(map.get(Consts.CFG_PARAM_EMPTY), Consts.CFG_PARAM_EMPTY_Y);
		String paramRange = StringUtils.trimNull(map.get(Consts.CFG_PARAM_RANGE));
		Date defaultDate = null;
		String defaultStr = BaseConst.EMPTY;
		if (dvalue.startsWith(Consts.CFG_PARAM_DVALUE_DATE_PRE)) {
			if (Consts.CFG_PARAM_DVALUE_DATE_TIME.equals(dvalue)) {
				request.putData(paramKey, DateUtils.getStrCurrtTime());
				return;
			} else if (Consts.CFG_PARAM_DVALUE_DATE_DATE.equals(dvalue)) {
				request.putData(paramKey, DateUtils.getStrCurrtDate());
				return;
			}
		} else if (dvalue.equals(Consts.CFG_PARAM_DVALUE_DATE_TIME)) {
			defaultDate = new Date(System.currentTimeMillis());
			defaultStr = DateUtils.getStrCurrtTime();
		} else if (dvalue.equals(Consts.CFG_PARAM_DVALUE_DATE_DATE)) {
			defaultDate = new Date(System.currentTimeMillis());
			defaultStr = DateUtils.getStrCurrtDate();
		}
		Date value = defaultDate;
		String valueStr = StringUtils.trimNull(request.getData(paramKey), defaultStr);
		// 日期转换
		if (paramRange.length() > 0) {
			DateFormat dateFormat = new java.text.SimpleDateFormat(paramRange);
			try {
				value = dateFormat.parse(valueStr);
			} catch (ParseException e) {
				value = defaultDate;
			}
			if (value != null) {
				valueStr = dateFormat.format(value);
			}
		}
		// 非空校验
		if (Consts.CFG_PARAM_EMPTY_N.equals(paramEmpty) && value == null) {
			// reutrn
			response.setCode(BusinessConst.RES_CODE_ERROR_BUIS_PARAM);
			response.setMsg(paramName + "不能为空！");
			response.putExtData(Consts.VALIDATE_RS_KEY, paramKey);
			response.putExtData(Consts.VALIDATE_RS_TYPE, Consts.VALIDATE_RS_TYPE_EMPTY);
			return;
		}
		paramRlue(request, response, map, valueStr);
		request.putData(paramKey, valueStr);

	}

	protected void paramRlue(BusinessRequest request, BusinessResponse response, Map<String, Object> map,
			Object value) {
		String paramRlue = StringUtils.trimNull(map.get(Consts.CFG_PARAM_RULE));
		if (paramRlue.length() > 0) {

		}
	}

}
